package cn.lh.controller;


import cn.lh.config.Resp;
import cn.lh.entity.req.LoginReq;
import cn.lh.entity.req.UserReq;
import cn.lh.entity.vo.LoginInfoVo;
import cn.lh.entity.vo.UserVo;
import cn.lh.service.LoginService;
import cn.lh.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author linhong
 * @since 2023-12-15
 */
@Api(tags = "用户管理")
@Validated
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    @Resource
    private UserService userService;




    @ApiOperation(value = "新增用户", notes = "新增用户")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @PostMapping("/create")
    public Resp<?> createUser(@RequestBody @Valid UserReq req){
        userService.createUser(req);
        return ok();
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @PutMapping("/update")
    public Resp<?> updateUser(@RequestHeader String token, @RequestBody @Valid  UserReq req){
        userService.updateUser(req);
        return ok();
    }

    @ApiOperation(value = "用户列表", notes = "用户列表")
    @ApiImplicitParam(name = "name", value = "用户名",  paramType = "query", dataType = "String")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @GetMapping("/getList")
    public Resp<List<UserVo>> getList(@RequestHeader String token, String name){
        return ok(userService.getList(name));
    }


    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "path", dataType = "Integer")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @DeleteMapping("/delete/{userId}")
    public Resp<?> deleteUser(@RequestHeader String token,@PathVariable("userId") @Valid Integer userId){
        userService.removeById(userId);
        return ok();
    }


}

